﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class LevelPanel : BasePanelUI
{
    public Button backButton;
    public Transform gridTsf;
    public List<LevelItem> levelItems = new List<LevelItem>();
    public GameObject levelItemPrefab;
    public override void Init()
    {
        base.Init();
        rootRect.gameObject.SetActive(false);
        levelItemPrefab.gameObject.SetActive(false);
    }

    public override void BtnEvent()
    {
        base.BtnEvent();
        backButton.onClick.AddListener(() => {
            SoundManager.Instance.PlayAudio(SoundType.Click.ToString(), false);
            GameCtrl.Instance.startPanel.Show();
            Hide();
        });
    }

    public override void Show()
    {
        base.Show();
        UpdateLevelItem();
    }

    private void UpdateLevelItem()
    {
        List<LevelData> ldList = GameCtrl.Instance.levelDataList;
        int countData = ldList.Count;
        int countItem = levelItems.Count;
        for (int i = 0; i < countData; i++)
        {
            if (i >= countItem)
            {
                levelItems.Add(CreateLevelItem());
            }
            levelItems[i].Init(ldList[i].id);
        }
    }

    private LevelItem CreateLevelItem()
    {
        GameObject obj = GameObject.Instantiate(levelItemPrefab) as GameObject;
        obj.transform.parent = gridTsf;
        obj.transform.localScale = Vector3.one;
        obj.SetActive(true);
        LevelItem item = obj.GetComponent<LevelItem>();
        return item;
    }
}
